<template>
  <div style="overflow: hidden">
    <div>
      <el-form :inline="true"
               size="mini">
        <el-form-item>
          <date-picker-rang v-model="selectTime"></date-picker-rang>
        </el-form-item>

        <el-form-item>
          <el-input size="mini"
                    @keyup.enter.native="handleIconClick"
                    v-model="pagination.params.searchValue"
                    placeholder="座席姓名.工号/客户姓名.号码"
                    clearable>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     @click="handleIconClick">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary"
                     @click="exportToExcel">导出excel</el-button>
        </el-form-item>
      </el-form>
    </div>

    <dd-table height="calc(100vh - 200px)"
              :data="tableData"
              :config="config"
              border
              :loading="loading"
              @filter-change="filterHandler"
              :current-page="pagination.currentPage"
              :total="pagination.total"
              @current-change="changePage"
              :page-size="pagination.pageSize"
              @size-change="handleSizeChange">
    </dd-table>

  </div>
</template>
<script>
import commonApi from '@/api/common.js'
import { Notification } from 'element-ui'
// 客服中心交互明细表
export default {
  data () {
    return {
      filterList: [],
      tableData: [],
      loading: false,
      config: [
        {
          label: '序号',
          prop: 'indexnum',
          width: '60'
        },
        {
          label: '日期',
          prop: 'ldate',
          'show-overflow-tooltip': true
        },
        {
          label: '交互ID',
          prop: 'interactId',
          'show-overflow-tooltip': true
        },
        {
          label: '客户姓名',
          prop: 'custName',
          'show-overflow-tooltip': true
        },
        {
          label: '客户号',
          prop: 'custNo',
          'show-overflow-tooltip': true
        },
        {
          label: '客户等级',
          prop: 'custLevel',
          'show-overflow-tooltip': true
        },
        {
          label: '渠道',
          prop: 'channel',
          'show-overflow-tooltip': true,
          'column-key': 'group',
          filters: []
        },
        {
          label: '录音号',
          prop: 'recordId',
          'show-overflow-tooltip': true
        },
        {
          label: '来电号码',
          prop: 'callerNo',
          'show-overflow-tooltip': true
        },
        {
          label: '通话开始时间',
          prop: 'beginTime',
          'show-overflow-tooltip': true
        },
        {
          label: '通话结束时间',
          prop: 'endTime',
          'show-overflow-tooltip': true
        },
        {
          label: '通话时长',
          prop: 'callDuration',
          'show-overflow-tooltip': true
        },
        {
          label: '座席工号',
          prop: 'agentId',
          'show-overflow-tooltip': true
        },
        {
          label: '座席姓名',
          prop: 'agentName',
          'show-overflow-tooltip': true
        },
        {
          label: '座席按钮操作',
          prop: 'busiDisatis',
          'show-overflow-tooltip': true
        },
        {
          label: '小结分类',
          prop: 'summaryClassify',
          'show-overflow-tooltip': true
        },
        {
          label: '小结内容',
          prop: 'summaryContent',
          'show-overflow-tooltip': true
        }
      ],
      selectTime: ['', ''],
      pagination: {
        currentPage: 1,
        total: 0,
        pageSize: 20,
        // 0 不导出 1导出
        isExport: '0',
        params: {
          searchValue: '',
          selectTime: ['', ''],
          group: []
        }
      }
    }
  },
  created () { },
  watch: {},
  mounted () {
    this.pagination.params.selectTime = ['', '']
    this.getTableData()
  },
  computed: {
    getHeight () {
      return this.$store.state.common.tableHeight
    }
  },
  methods: {
    // 导出excel表
    exportToExcel () {
      this.pagination.isExport = '1'
      let tHeader = []
      let filterVal = []
      for (let item of this.config) {
        filterVal.push(item.prop)
        tHeader.push(item.label)
      }
      commonApi
        .getCustomCenterInterDetail(this.pagination)
        .then(response => {
          if (response.retcode === '0') {
            require.ensure([], () => {
              const { export_json_to_excel } = require('@/utils/vendor/Export2Excel.js')
              const list = response.data.info
              const data = this.formatJson(filterVal, list)
              export_json_to_excel(
                tHeader,
                data,
                '客服中心交互明细表'
              )
            })
          } else {
            Notification.error('数据返回失败')
          }
        })
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    filterHandler (val) {
      this.pagination.params.group = val['group']
      this.pagination.isExport = '0'
      this.getTableData()
    },
    // 查询按钮
    handleIconClick () {
      this.getTableData()
    },
    // 切换页码
    changePage (val) {
      this.pagination.currentPage = val
      this.getTableData()
    },
    // 可以调整每页条数
    handleSizeChange (val) {
      this.pagination.pageSize = val
      this.getTableData()
    },
    // 获取数据表
    getTableData () {
      this.loading = true
      this.pagination.isExport = '0'
      this.pagination.params.selectTime[0] =
        this.selectTime[0] !== ''
          ? this.formmatDate(this.selectTime[0], 'yyyy-MM-dd')
          : ''
      this.pagination.params.selectTime[1] =
        this.selectTime[1] !== ''
          ? this.formmatDate(this.selectTime[1], 'yyyy-MM-dd')
          : ''
      commonApi
        .getCustomCenterInterDetail(this.pagination)
        .then(response => {
          this.loading = false
          if (response.retcode === '0') {
            this.tableData = response.data.info
            this.pagination.total = response.data.total
            this.filterList = response.data.filterList
          } else {
            Notification.error('数据返回失败')
          }
        })
    }
  }
}
</script>
